#pragma once
#include "Comm.h"

#if defined(__GNUC__) && !defined(__CYGWIN__)
#include <bits/local_lim.h>
#endif
#ifndef SEM_VALUE_MAX
#define SEM_VALUE_MAX   (0x7FFFFFFF)
#endif

namespace COMM
{
	class COMM_API ThreadSemaphore
	{
		MYLIB_NONCOPIED_CLASS(ThreadSemaphore);
	public:
		// nInitValue range: 0 - SEM_VALUE_MAX.
		ThreadSemaphore(unsigned int nInitValue = 0);
		~ThreadSemaphore(void);

		// increase semaphore value.
		// the current semaphore value can not exceed SEM_VALUE_MAX.
		void SetSignal();
		// decrease semaphore value. 
		// block thread while the current semaphore value is zero.
		bool WaitSignal(unsigned int timeoutMs = -1);

	private:
#ifdef COMM_MSVC
		HANDLE m_sem;
#else
		sem_t m_sem;
#endif
	};

}

